/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.mes.service.impl;

import me.zhengjie.modules.mes.domain.MesPageConfig;
import me.zhengjie.modules.mes.domain.vo.MesPageConfigVo;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.mes.service.MesPageConfigService;
import me.zhengjie.modules.mes.domain.vo.MesPageConfigQueryCriteria;
import me.zhengjie.modules.mes.mapper.MesPageConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author mfc
* @date 2024-12-31
**/
@Service
@RequiredArgsConstructor
public class MesPageConfigServiceImpl extends ServiceImpl<MesPageConfigMapper, MesPageConfig> implements MesPageConfigService {

    private final MesPageConfigMapper mesPageConfigMapper;

    @Override
    public PageResult<MesPageConfig> queryAll(MesPageConfigQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(mesPageConfigMapper.findAll(criteria, page));
    }

    @Override
    public List<MesPageConfig> queryAll(MesPageConfigQueryCriteria criteria){
        return mesPageConfigMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(MesPageConfig resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MesPageConfig resources) {
        MesPageConfig mesPageConfig = getById(resources.getId());
        mesPageConfig.copy(resources);
        saveOrUpdate(mesPageConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<MesPageConfig> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MesPageConfig mesPageConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("目标功能1", mesPageConfig.getTargetFunctionId1());
            map.put("目标功能2", mesPageConfig.getTargetFunctionId2());
            map.put("目标功能3", mesPageConfig.getTargetFunctionId3());
            map.put("页面类型1：一级页面2：非一级", mesPageConfig.getPageType());
            map.put("模版程序", mesPageConfig.getTemplatePage());
            map.put("页面链接", mesPageConfig.getPageLink());
            map.put("状态", mesPageConfig.getStatus());
            map.put("生成时间", mesPageConfig.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public PageResult<MesPageConfigVo> queryAllByCriterias(MesPageConfigQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(mesPageConfigMapper.findAllByCriteria(criteria, page));
    }
}